package com.hs.auth.action;
 
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.auth.AUTHConstant;
import com.hs.auth.model.HsAccount;
import com.hs.auth.service.IUserService;
import com.hs.core.action.BaseAction;
import com.hs.core.utils.MD5;
@Controller  
public class LogController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(LogController.class);
	private IUserService userService;
	 @Autowired  
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	 /**
	  * 用户登录
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */ 
	@RequestMapping(value="/logon.do")   
	public ModelAndView logon(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException { 
		StringBuffer buffer = new StringBuffer();
		String viewname = "logon";
		String uid = request.getParameter("UserName");
		String password = request.getParameter("Password");
		String rememberme = request.getParameter("rememberme");
		logger.debug("rememberme:" + rememberme);
		if("1".equals(rememberme)){
			rememberMe(uid,password,response);
		}
		HsAccount obj = userService.getAccByUid(uid);
		
		//根据用户名查询帐号信息
		if(null != obj){
			String dbpassword = obj.getPassword();
			String inputpwd = MD5.md5(password);
			if(inputpwd.equals(dbpassword)){
				viewname = "app/index";
				this.initSession(request, obj);
				this.loadAuth(request,obj.getId()+"");
			} else{
				buffer.append("密码输入错误!");
			}
		}else{
			 buffer.append("【").append(uid).append("】").append("不存在!");
		}   
		
		modelMap.put("alert", buffer.toString());
		modelMap.put("username", uid);
		return new ModelAndView(viewname,modelMap);  
	}
	private void rememberMe(String uid,String password,HttpServletResponse response){
		Cookie c = new Cookie("dnmsabname",uid); 
 		Cookie pwd = new Cookie("dnmsabpwd",password); 

 		c.setMaxAge(30*24*60*60); //设置过期之前的最长时间    
  		//设置Cookie路径和域名 
 		//c.setDomain("cintcm.ac.cn") ;
 		response.addCookie(c); 
 		c.setMaxAge(30*24*60*60); //设置过期之前的最长时间    
  		//设置Cookie路径和域名 
 		//c.setDomain("cintcm.ac.cn") ; 
 		response.addCookie(pwd);
 		pwd.setMaxAge(30*24*60*60);
	}
	 /**
	  * 用户登出
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/logout.do")   
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
		StringBuffer buffer = new StringBuffer(); 
		logger.info("In the logController logout method!");
		HttpSession session = request.getSession();
		String userId = "";
		String userName = "";
		HsAccount accinfo = (HsAccount)session.getAttribute(AUTHConstant.USERINSESSION);
		if(null != accinfo){
			userId = accinfo.getId()+"";
 		}
		session.setAttribute(AUTHConstant.USERINSESSION, null); 
		session.setAttribute("USERPOWER_" + userId, null);
		//LogUtil.log(req,userName+"-退出系统",LogUtil.LOG_OUT,userId);
		if(null != session)
			session.invalidate(); 
		return new ModelAndView("/logon");  
	}
	 
	 
	 
}
